<?php

require_once dirname(__FILE__) . '/../lib/BaselzchatActions.class.php';

/**
 * lzchat actions.
 * 
 * @package    sfLoginzaPropelPlugin
 * @subpackage lzchat
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12534 2008-11-01 13:38:27Z Kris.Wallsmith $
 */
class lzchatActions extends BaselzchatActions {

    public function executeIndex(sfWebRequest $request) {

        $this->user = $this->getUser();

        if ($this->user->hasAttribute('first_name')) {
            $this->full_name = $this->user->getAttribute('first_name') . ' ' . $this->user->getAttribute('last_name');
        } else if($this->user->hasAttribute('nickname')) {
            $this->full_name = $this->user->getAttribute('nickname');
        }else if ($this->user->hasAttribute('email')) {
            $this->full_name = $this->user->getAttribute('email');
        } else {
            $this->full_name = $this->user->getAttribute('identity');
        }

        //$ch_user = LzUserPeer::retrieveByPK($this->user->getAttribute('id')); //get current user from db
        // select all LzUsers from db but current user
        $criteria = new Criteria();
        $criteria->add(LzUserPeer::ID, $this->user->getAttribute('id'), Criteria::NOT_EQUAL);
        $this->users = LzUserPeer::doSelect($criteria);
    }

    /*
     * get all messages from 'id_user' to 'id_user_with' and from 'id_user_with' to 'id_user'
     * or get new messages from 'id_user_with' to 'id_user'
     */

    public function executeGetMessages(sfWebRequest $request) {

        $criteria = new Criteria();
        $getMessagesType = $request->getParameter('type');

        if ($getMessagesType == 'all') {
            $criteria1 = $criteria->getNewCriterion(LzMsgPeer::ID_FROM, $request->getParameter('id_user'), Criteria::EQUAL);
            $criteria1->addAnd($criteria->getNewCriterion(LzMsgPeer::ID_TO, $request->getParameter('id_user_with'), Criteria::EQUAL));
            $criteria2 = $criteria->getNewCriterion(LzMsgPeer::ID_FROM, $request->getParameter('id_user_with'), Criteria::EQUAL);
            $criteria2->addAnd($criteria->getNewCriterion(LzMsgPeer::ID_TO, $request->getParameter('id_user'), Criteria::EQUAL));

            $criteria1->addOr($criteria2);
            $criteria->add($criteria1);
        } else if ($getMessagesType == 'new') {
            $criteria->add(LzMsgPeer::ID_FROM, $request->getParameter('id_user_with'), Criteria::EQUAL);
            $criteria->addAnd($criteria->getNewCriterion(LzMsgPeer::ID_TO, $request->getParameter('id_user'), Criteria::EQUAL));
            $criteria->addAnd($criteria->getNewCriterion(LzMsgPeer::READED, false, Criteria::EQUAL));
        } else {
            echo 'getMessages: Error: type is not defined';
            exit();
        }

        $criteria->addAscendingOrderByColumn(LzMsgPeer::TIME); //sorting by time field

        $messages = LzMsgPeer::doSelect($criteria);

        $response = array();
        $i = 0;

        foreach ($messages as $msg) {
            $response[$i]['id'] = $msg->getId();
            $response[$i]['id_from'] = $msg->getIdFrom();
            $response[$i]['id_to'] = $msg->getIdTo();
            $response[$i]['letter'] = $msg->getLetter(); //iconv('cp1251','UTF-8',$msg->getLetter());
            $response[$i]['data'] = $msg->getDate();

            //do 'readed' messages from 'id_user_with' to 'id_user'
            if (!$msg->getReaded() && $msg->getIdFrom() != $request->getParameter('id_user')) {
                $msg->setReaded(true);
                $msg->save();
            }

            $i++;
        }

        echo json_encode($response); //convert to JSON format

        return sfView::NONE; //without view
    }

    /*
     * add message from 'id_from' to 'id_to'
     */

    public function executeAddMessage(sfWebRequest $request) {
        $id_from = $request->getParameter('id_from');
        $id_to = $request->getParameter('id_to');
        $text = $request->getParameter('text');

        $new_message = new LzMsg();
        $new_message->setIdFrom($id_from);
        $new_message->setIdTo($id_to);
        $new_message->setLetter($text);
        $new_message->setTime(time());
        $new_message->setDate(date('Y-M-d H:i:s'));
        $new_message->setReaded(false);
        $new_message->save();

        return sfView::NONE;
    }

    /*
     * prolongation current user's status for 'online'
     */

    public function executeProlongationMyStatus() {
        $id = $this->getUser()->getAttribute('id');
        $user = LzUserPeer::retrieveByPK($id);
        $user->setExpiresAt($this->getExpiresAtTime());
        $user->save();

        return sfView::NONE;
    }

    protected function getExpiresAtTime() {
        return time() + sfConfig::get('app_LoginzaPlugin_expires_after_sec');
    }

    /*
     * get ids of 'online' users but current
     */

    public function executeRefreshingUsersStatuses() {
        $id = $this->getUser()->getAttribute('id');
        $criteria = new Criteria();
        $criteria->add(LzUserPeer::EXPIRES_AT, time(), Criteria::GREATER_THAN);
        $criteria->addAnd(LzUserPeer::ID, $id, Criteria::NOT_EQUAL);

        $users_online = LzUserPeer::doSelect($criteria);

        $response = array();
        $i = 0;

        foreach ($users_online as $usr) {
            $response[$i]['id'] = $usr->getId();
            $i++;
        }

        echo json_encode($response); // convert to JSON

        return sfView::NONE;
    }

}
